﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using ChuanYe.CountyFireManagement.Model;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireHeadquarterBLL
    {
        #region 构造单例
        private FireHeadquarterBLL() { }
        private static FireHeadquarterBLL _instance;
        public static FireHeadquarterBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireHeadquarterBLL());
            }
        }
        #endregion

        public int Add(Fireheadquarter info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Fireheadquarter.Where(p => p.Name == info.Name).FirstOrDefault();
                if (exist != null)
                {
                    return -2;
                }
                ef.Fireheadquarter.Add(info);
                ef.Entry<Fireheadquarter>(info).State = EntityState.Added;
                ef.SaveChanges();
                return 1;
            }
        }

        public List<Fireheadquarter> Get(string addressCounty, string addressTown, string addressVillage)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Fireheadquarter select a;
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    linq = linq.Where(p=>p.AddressCounty==addressCounty);
                }
                if (!string.IsNullOrEmpty(addressTown))
                {
                    linq = linq.Where(p => p.AddressTown == addressTown);
                }
                if (!string.IsNullOrEmpty(addressVillage))
                {
                    linq = linq.Where(p => p.AddressVillage == addressVillage);
                }
                return linq.OrderBy(p=>p.Sort).ToList();
            }
        }

        public ResultDto<dynamic, dynamic> GetByPage(string addressCounty, string addressTown, string addressVillage, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Fireheadquarter select a;
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    linq = linq.Where(p => p.AddressCounty == addressCounty);
                }
                if (!string.IsNullOrEmpty(addressTown))
                {
                    linq = linq.Where(p => p.AddressTown == addressTown);
                }
                if (!string.IsNullOrEmpty(addressVillage))
                {
                    linq = linq.Where(p => p.AddressVillage == addressVillage);
                }

                int startRow = page.PageSize * (page.PageIndex - 1);
                var list = linq.OrderBy(o => o.Sort).ThenByDescending(t => t.HeadquarterId)
                               .Skip(startRow).Take(page.PageSize).ToList();

                var linq2 = from a in list
                            join b in ef.Linchpin on a.AddressCounty equals b.LinchpinId.ToString()
                            into c
                            let d = c.FirstOrDefault()
                            join e in ef.Linchpin on a.AddressTown equals e.LinchpinId.ToString()
                            into f
                            let g = f.FirstOrDefault()
                            join h in ef.Linchpin on a.AddressVillage equals h.LinchpinId.ToString()
                            into i
                            let j = i.FirstOrDefault()
                            select new
                            {
                                a.HeadquarterId,
                                a.ParentId,
                                a.Tel,
                                a.Name,
                                a.Sort,
                                a.Ip,
                                a.AddressCounty,
                                a.AddressTown,
                                a.AddressVillage,
                                AddressCountyName =d==null?string.Empty:d.LinchpinName,
                                AddressTownName= g == null ? string.Empty : g.LinchpinName,
                                AddressVillageName= j== null ? string.Empty : j.LinchpinName,
                            };

                result.FirstParam = linq2.ToList();
                result.SecondParam = linq.Count();
                return result;
            }
        }

        public int Update(Fireheadquarter info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Fireheadquarter.Where(p => p.HeadquarterId == info.HeadquarterId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Fireheadquarter.Where(p => p.HeadquarterId != info.HeadquarterId && p.Name == info.Name).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.Name = info.Name;
                    exist.ParentId = info.ParentId;
                    exist.Sort = info.Sort;
                    exist.Tel = info.Tel;
                    exist.Ip = info.Ip;
                    exist.AddressCounty = info.AddressCounty;
                    exist.AddressTown = info.AddressTown;
                    exist.AddressVillage = info.AddressVillage;
                    exist.TransDatetime = info.TransDatetime;
                    ef.Fireheadquarter.Add(exist);
                    ef.Entry<Fireheadquarter>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public int Remove(int headquarterId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {

                var exist = ef.Fireheadquarter.Where(p => p.HeadquarterId == headquarterId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }

                //是否被扑救力量所占用
                var existFirefightingforce = ef.Firefightingforce.Where(p => p.HeadquarterId == headquarterId).FirstOrDefault();
                if(existFirefightingforce!=null)
                {
                    return -2;
                }

                //是否被指挥部成员所占用
                var existFireheadquartermember = ef.Fireheadquartermember.Where(p => p.HeadquarterId == headquarterId).FirstOrDefault();
                if (existFireheadquartermember != null)
                {
                    return -3;
                }

                ef.Fireheadquarter.Add(exist);
                ef.Entry<Fireheadquarter>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();
            }
        }
    }
}
